package com.demo.impala;

import java.sql.*;

public class ImpalaTemplate {
    static Connection connection;
    static {
        try {
            Class.forName("com.cloudera.impala.jdbc41.Driver");
            connection = DriverManager.getConnection("jdbc:impala://hadoop-3:21050/default");

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        selectImpala();
        closeImpalaConn();
    }

    private static void closeImpalaConn() {
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    private static void selectImpala() {
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            statement = connection.createStatement();
            boolean execute = false;
            statement.execute("INVALIDATE METADATA");
            execute = statement.execute("SELECT * FROM table_hive_to_impala2");
            if(execute){
                resultSet = statement.getResultSet();
                while (resultSet.next()){
                    String string = resultSet.getString(1);
                    System.out.println(string);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                if(resultSet != null){
                    resultSet.close();
                }
                statement.close();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }


}
